from PIL import Image
from qrcode import QRCode, constants
from qrcode.image.pure import PymagingImage
from PySide6.QtGui import QImage, QPixmap


def create_qr_code(content, label):
    qr = QRCode(
        version=1, error_correction=constants.ERROR_CORRECT_H, box_size=4, border=4
    )
    qr.add_data(content)
    qr.make(fit=True)

    img = qr.make_image(fill="black", back_color="white")
    # --------------------------------
    # 添加小图标
    # esize = img.size
    # ew, eh = esize
    # s = 6
    # icon = Image.open("wx.png")
    # iw, ih = int(ew / s), int(eh / s)
    # icon_s = icon.resize((iw, ih), Image.ANTIALIAS)
    # ix, iy = int((ew - iw) / 2), int((eh - ih) / 2)
    # img.paste(icon_s, (ix, iy))
    # ------------------------------
    img.save("qr_code.png")
    # pixmap = QPixmap.fromImage(QImage(img))
    label.setPixmap(QPixmap("qr_code.png"))

    # img_data = img.convert("RGBA").tobytes("raw", "RGBA")
    # qr_image = PymagingImage(img, width=100, height=100, box_size=10, border=4)
    # pixmap = QPixmap.fromImage(img_data)
    # label.setPixmap(pixmap)
    # label.setPixmap(qr_image.pixmap())
